<?php
/**
 * Created by DRCOM.
 * User: GZX
 * Datetime: 2021-08-18 13:31
 */

namespace app\api\controller;

use think\Db;


class Taoke
{
    /**
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * _100x100_.webp
     */
    public function index()
    {
        $user = db('user')->where('username', 'admin')->find();
        $tbId = explode(",", $user['tb_id']);
        $orders = Db::table('tbl_order_details')->whereIn("tb_id", $tbId)
            ->where('adzone_id', $user['adzone_id'])
            ->select();
        dump($orders);
    }

    public function orderDetails()
    {
//        debug('begin');
//        config('app_trace', 1);
        //实时订单查询
        $startTime = date('Y-m-d H:i:00', strtotime('-1 minute'));
        $endTime = date('Y-m-d H:i:00');
        echo($startTime . "\t" . $endTime);
        $now = order_details($startTime, $endTime);
        $count = count($now);
        if ($count) {
//            $todayCount = Db::table('tbl_order_details')
//                ->whereTime('tb_paid_time', 'today')
//                ->where('tk_status', '<>', 13)
//                ->count();
//            $todaySum = Db::table('tbl_order_details')
//                ->whereTime('tb_paid_time', 'today')
//                ->where('tk_status', '<>', 13)
//                ->sum('pub_share_pre_fee');
//            $content = "今天的有效订单数：$todayCount\r\n预估收益：" . round($todaySum, 2);
            $content = $this->orderToday();
//        echo $content;
            db('wechatTmplmsg')->insert(['title' => '订单提醒', 'content' => $content]);
        }
        echo " 【" . $count . "】<br>";

        //最近20分钟订单
        $startTime = date('Y-m-d H:i:00', strtotime('-20 minute'));
        $endTime = date('Y-m-d H:i:00');
        echo($startTime . "\t" . $endTime);
        $now = order_details($startTime, $endTime);
        $count = count($now);
        echo " 【" . $count . "】<br>";

        //特殊节日停止遍历订单
        $specialDay = ['0618', '1111', '1212'];
        if (in_array(date('md'), $specialDay)) {
            exit;
        }

        //最近3小时订单
//        $startTime = date('Y-m-d H:i:00', strtotime('-200 minute'));
//        $endTime = date('Y-m-d H:i:00', strtotime('-20 minute'));
//        echo($startTime . "\t" . $endTime);
//        $now = order_details($startTime, $endTime);
//        $count = count($now);
//        echo " - " . $count . "<br>";

        //最近订单
        for ($i = 20; $i <= 3 * 24 * 60 + 20; $i = $i + 180) {
            $j = $i + 180;
            $startTime = date('Y-m-d H:i:00', strtotime("-{$j} minute"));
            $endTime = date('Y-m-d H:i:00', strtotime("-{$i} minute"));
            echo($startTime . "\t" . $endTime);
            $now = order_details($startTime, $endTime);
            $count = count($now);
            echo " 【" . $count . "】<br>";
        }

        //历史订单查询
        $currDate = strtotime(date('Y-m-d'));
        $currMin = strtotime(date('Y-m-d H:i'));
        $endTime = $currDate - (($currMin - $currDate) * 180);
        $startTime = date('Y-m-d H:i:s', $endTime - 10800);
        $endTime = date('Y-m-d H:i:s', $endTime);
        echo($startTime . "\t" . $endTime);
        $history = order_details($startTime, $endTime);
        $count = count($history);
        echo " 【" . $count . "】<br>";
//        再次查找当前订单
        $startTime = date('Y-m-d H:i:00', strtotime('-1 minute'));
        $endTime = date('Y-m-d H:i:s');
        echo($startTime . "\t" . $endTime);
        $now = order_details($startTime, $endTime);
        $count = count($now);
        echo " 【" . $count . "】<br>";

//        debug('end');
//        // 进行统计区间
//        echo debug('begin', 'end', 6) . 's<br>';
//        echo debug('begin', 'end', 'm');

    }

    private function orderToday()
    {
        $today = Db::table('tbl_order_today')->select();
        $msg = '';
        $order_num = $alipay_total_price = $pub_share_pre_fee = 0;
        foreach ($today as $row) {
            $temple = " 笔数:" . round($row['order_num'], 2) .
                "  金额:" . round($row['alipay_total_price'], 2) .
                "  收益:" . round($row['pub_share_pre_fee'], 2);

            switch ($row['tk_status']) {
                case 12:
                    $msg .= "订单付款:\r\n $temple\r\n";
                    break;
                case 14:
                    $msg .= "订单成功:\r\n $temple\r\n";
                    break;
                case 3:
                    $msg .= "订单结算:\r\n $temple\r\n";
                    break;
                case 13:
                    $msg .= "订单失效:\r\n $temple\r\n";
                    break;
                default:
                    $msg .= "error\r\n";
            }
            $order_num += $row['order_num'];
            $alipay_total_price += $row['alipay_total_price'];
            $pub_share_pre_fee += $row['pub_share_pre_fee'];
        }

        $temple = " 笔数:" . round($order_num, 2) .
            "  金额:" . round($alipay_total_price, 2) .
            "  收益:" . round($pub_share_pre_fee, 2);
        $msg .= str_repeat('-', 30) . "\r\n";
        $msg .= "$temple\r\n";
        return $msg;
    }

    //实时线报数据
    public function wireReport()
    {
        debug('begin');
        $hdk = wire_report();
        $data = json_decode($hdk, true);

        if ($data['code'] == 200 && isset($data['data'])) {
            foreach ($data['data'] as $row) {
                if (empty($row['step'])) {
                    $row['step'] = '';
                } else {
                    $row['step'] = json_encode($row['step'], 256);
                }
                $insert = db('wireReport')->strict(false)->insert($row, true);

            }
        }
        debug('end');
        // 进行统计区间
        echo debug('begin', 'end', 6) . 's<br>';
        echo debug('begin', 'end', 'm');
    }

    //好单提醒
    public function wireWarning(){

        $itemlike = config('site.itemlike');
        $itemlike = explode(";",$itemlike);
        $minId = (int)cache('wire_id');
        $maxId = \db('wireReport')->order('wire_id DESC')->value('wire_id');
        foreach ($itemlike as $like){
            $items = \db('wireReport')
                ->where('content','like',"%$like%")
                ->where('wire_id','between',[$minId,$maxId])
                ->order('wire_id DESC')
                ->limit(100)
                ->select();
            foreach ($items as $item){
                db('wechatTmplmsg')
                    ->insert(['title' => $like, 'content' => $item['content']]);
            }

        }

        cache('wire_id',$maxId);
        echo $maxId;
    }

}